import {
    userProfilesAPI,
} from "@/request/api";

export default {
    namespaced: true,
    state: {
        //购物车商品数量
        cartTotal: 0,
        //用户信息
        userInfo: {
            //昵称
            nickName: '游客22',
            //头像
            headImg: require('@/assets/img/userImg.f8bbec5e.png'),
            //积分
            coin: 100

        }
    },
    mutations: {

        //修改state的值  payload为actions提交的userInfoRes.data
        chanUserInfo(state, payload) {
            //购物车商品数量
            state.cartTotal = payload.cartTotal;
            //用户信息
            state.userInfo = payload.userInfo;
        },
        //如果没有token显示默认的user信息
        initUserInfo(state) {
            //购物车商品数量
            state.cartTotal = 0;
            //用户信息
            state.userInfo = {
                //昵称
                nickName: '游客22',
                //头像
                headImg: require('@/assets/img/userImg.f8bbec5e.png'),
                //积分
                coin: 100,
            }
        },
    },
    getters: {
    },

    actions: {
        //修改state值的异步操作发在actions里
        async asyncChanUserInfo({ commit }) {
            //向后端发起请求获取用户信息
            let userInfoRes = await userProfilesAPI();
            //给mutations里的chanUserInfo方法提交用户信息数据
            commit('chanUserInfo', userInfoRes.data)
            console.log('user----', userInfoRes.data);
        }
    },


}